package com.googlecode.tda367.denty.ctrl;



import org.jbox2d.common.Vec2;

import com.googlecode.tda367.denty.constants.DynamicConstant;
import com.googlecode.tda367.denty.constants.StateConstant;
import com.googlecode.tda367.denty.core.dynamicbody.MoveableBody;

/**
 * <p>This class contains methods that convert input from outside 
 * (user input or program generated) concerning movement of in-game objects (DynamicBody-ies).
 * </p>
 * <p>Manipulation of in-game objects go through these methods: DynamicBody.applyForce() 
 * and DynamicBody.applyVelocity(). Any method here should be named after what the input source
 * intends to do, and use the methods from DynamicBody accordingly.</p>
 * <p>Where applicable: Distinguish whether the specific call is valid (is the parameter db allowed to make this move?)
 * and find any constants that may apply to the situation. Validation can be done through <code>instanceof</code>, but 
 * preferably by asking a yet-to-be-constructed constants class. </p>
 * 
 * <p> Example of method construction:<br>
 * <code>public void jump(DynamicBody db){<br>
 * if(DynamicConstants.allowedJump(db.getID())){ // yet to be implemented, up for discussion<br>
 * float jumpForce = DynamicConstans.getJumpForce(db.getID()); // another way to validate jump <br>
 * db.applyForce(jumpForce); // or db.applyForce(3000.0f) if the first check is used solely.<br> 
 * }</code><br>
 * <br>
 * As you can see, the above method contains some unimplemented guidelines. 
 * The clever thing is that object constants can be defined to variate these general methods
 * as different variables may apply to them each.
 * Another approach is to add a <code>public boolean permissions(@PermissionType) // PermissionType - an enum. </code>
 * to the DynamicBody instead of an externalized constants class, and a similar one to get dynamic constants.
 * 
 * @author Max Witt
 *
 */
public class MoveController {
	
		
	public static void moveLeft(MoveableBody db){
		db.setState(StateConstant.MOVING_LEFT, true);
		Vec2 dbLinearVelocity = db.getLinearVelocity();
		if(dbLinearVelocity.x > - db.getConstant(DynamicConstant.VELOCITY_H_MAX))
			dbLinearVelocity.x -= db.getConstant(DynamicConstant.VELOCITY_H_INC);
		db.applyVelocity(dbLinearVelocity);
	}
	public static void moveRight(MoveableBody db){
		db.setState(StateConstant.MOVING_RIGHT, true);
		Vec2 dbLinearVelocity = db.getLinearVelocity();
		if(dbLinearVelocity.x < db.getConstant(DynamicConstant.VELOCITY_H_MAX))
			dbLinearVelocity.x += db.getConstant(DynamicConstant.VELOCITY_H_INC);
		db.applyVelocity(dbLinearVelocity);
	}
	
	public static void jump (MoveableBody db){
		Vec2 jumpForce = new Vec2(0, 0);
		if (db.getLinearVelocity().y==0){
			jumpForce.y += db.getConstant(DynamicConstant.JUMP_FORCE);
		}
		db.applyForce(jumpForce);
		
	}
	
	public static void throwBody(MoveableBody db, Vec2 force){
		//TODO: Check for maximum forces and correct
		db.applyForce(force);
	}
	public static void moveTowards(MoveableBody db, Vec2 dentyPosition) {
		Vec2 dbPosition = db.getPosition();
		if (dbPosition.x < dentyPosition.x ) {
			moveRight(db);
			
		} else if (dbPosition.x > dentyPosition.x) {
			moveLeft(db);
		}
		
	}
	
	
}
